<template>
    <div :class="computedClass">
        <div class="messageToogle"></div>
        <div class="content" v-html="computedText"></div>
        <MessageStatusComponent/>
    </div>
</template>
<script lang="ts" setup>
import { computed, inject } from 'vue'
import { getImgTags, getImgPathByTag } from '@/assets/images/emotion/index.ts'
import MessageStatusComponent from './MessageStatusComponent.vue'; 
const chat = inject('chat', {})
const align = inject('align', 'left')
const tags = getImgTags()
const computedClass = computed(()=> ['messageTextWrapper', align == 'right' ? 'right' : ''])
const computedText = computed(()=> replaceImg(chat.message))
const replaceImg = (text: string) => {
  // 创建一个正则表达式来匹配所有特殊字符
  const regex = new RegExp(tags.join('|'), 'g');
  // 替换文本中的特殊字符为对应的图片
  const replacedText = text ? text.replace(regex, (match) => {
    const imgElement = document.createElement('img');
    imgElement.src = getImgPathByTag(match);
    imgElement.alt = match;
    imgElement.style.width = '15px';
    imgElement.style.height = '15px';
    return imgElement.outerHTML;
  }) : '';

  return replacedText;
}
</script>
<style lang="less" scoped>
.messageTextWrapper {
  display: flex;
  width: 70%;
  user-select:auto;
  &.right {
    flex-direction: row-reverse;
      .messageToogle {
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 8px 8px 8px 8px; /* 左侧边框宽度决定三角大小 */
        border-color: transparent transparent transparent #6fe648; /* 左侧边框颜色与消息背景色相同 */
        position: relative;
        top: 9px;
      }
      .content {
        background-color: #6fe648;
      }
  }
  .messageToogle {
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 8px 8px 8px 0; /* 右侧边框宽度决定三角大小 */
    border-color: transparent white transparent transparent;
    position: relative;
    top: 9px;
    filter: drop-shadow(-2px 2px 4px #ececec);
    //box-shadow: -2px 2px 4px #ececec;
  }
  .content {
    background-color: white;
    padding: 7px 10px;
    border-radius: 3px;
    font-size: 13.5px;
    //filter: drop-shadow(2px 0px 10px #f1f1f1);
    box-shadow: 2px 2px 4px #ececec;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    max-width: 70%;
    font-size: 14px;
    color: #101010;
  }
}
</style>